<template>
  <div>
    <el-dialog
      :title="$t('logIn')"
      :visible.sync="loginFormVisible"
      width="30%"
      @close="$emit('closeDialog')"
    >
      <el-form
        :model="loginForm"
        :rules="loginRules"
        ref="loginForm"
        class="demo-loginForm"
      >
        <el-form-item :label="$t('email')" prop="email">
          <el-input
            v-model="loginForm.email"
            :placeholder="$t('email')"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('password')" prop="password">
          <el-input
            v-model="loginForm.password"
            :placeholder="$t('password')"
            type="password"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="danger"
          v-if="isShowToRegister"
          @click="$emit('register')"
          >{{ $t("toRegister") }}</el-button
        >
        <el-button @click="$emit('closeDialog')">{{ $t("cancel") }}</el-button>
        <el-button type="primary" @click="login('loginForm')">{{
          $t("logIn")
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { httpMobileLogin, httpInfo } from "@/http/api";
export default {
  name: "login",
  props: ["loginFormVisible", "isShowToRegister"],
  data() {
    return {
      loginForm: {},
      loginRules: {
        email: [
          {
            required: true,
            message: this.$t("emailTips"),
            trigger: "change",
          },
        ],
        password: [
          {
            required: true,
            message: this.$t("passwordTips"),
            trigger: "change",
          },
          {
            min: 8,
            max: 16,
            message: this.$t("passwordTips"),
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    login(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let params = {
            account_name: this.loginForm.email,
            password: this.loginForm.password,
            type: "email",
          };
          let res = await httpMobileLogin(params);
          if (res.code == 0) {
            localStorage.setItem("TOKEN", res.data.token);
            let res2 = await httpInfo();
            if (res2.code == 0) {
              localStorage.setItem("email", this.loginForm.email);
              localStorage.setItem("account_name", res2.data.account_name);
              localStorage.setItem("invite_code", res2.data.invite_code);
              localStorage.setItem("uid", res2.data.uid);
              this.$emit("loginSuccess");
              location.reload();
            }
          } else {
            this.$message({
              type: "error",
              message: res.message,
            });
            this.loginFormVisible = false;
          }
        } else {
          console.log("error");
        }
      });
    },
  },
  created() {
    console.log(this.loginFormVisible, 1111);
  },
};
</script>
